exec sp_executesql N'SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	RIGHT(REPLICATE(''0'', 4) + CONVERT(VARCHAR, CONVERT(BIGINT, B.PER_TARIFA * 100)), 4) +
	RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, B.TOTAL_TARIFA * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A 
INNER JOIN LOJA_CONTROLE_FISCAL_TARIFAS B ON A.CODIGO_FILIAL = B.CODIGO_FILIAL AND A.TERMINAL = B.TERMINAL AND A.ECF = B.ECF AND A.ID_EQUIPAMENTO = B.ID_EQUIPAMENTO AND A.DATA_FISCAL = B.DATA_FISCAL AND A.CF_INICIAL = B.CF_INICIAL AND A.SERIE_NF = B.SERIE_NF
WHERE
	A.CODIGO_FILIAL = @P1
	AND A.DATA_FISCAL BETWEEN @P2 AND @P3
	AND B.TOTAL_TARIFA > 0 AND B.LEGENDA_TARIFA NOT IN (''COFINS'', ''ISENTO'', ''PIS'')
UNION
SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	CONVERT(CHAR(4), ''F'') + RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, A.TOTAL_SUBSTITUICAO * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A
WHERE
	A.CODIGO_FILIAL = @P4
	AND A.DATA_FISCAL BETWEEN @P5 AND @P6
	AND A.TOTAL_SUBSTITUICAO > 0
UNION
SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	CONVERT(CHAR(4), ''I'') + RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, A.TOTAL_ISENTO * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A
WHERE
	A.CODIGO_FILIAL = @P7
	AND A.DATA_FISCAL BETWEEN @P8 AND @P9
	AND A.TOTAL_ISENTO > 0
UNION
SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	CONVERT(CHAR(4), ''N'') + RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, A.TOTAL_NAO_TRIBUTAVEL * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A
WHERE
	A.CODIGO_FILIAL = @P10
	AND A.DATA_FISCAL BETWEEN @P11 AND @P12
	AND A.TOTAL_NAO_TRIBUTAVEL > 0
UNION
SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	CONVERT(CHAR(4), ''CANC'') + RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, A.TOTAL_CANCELADO * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A
WHERE
	A.CODIGO_FILIAL = @P13
	AND A.DATA_FISCAL BETWEEN @P14 AND @P15
	AND A.TOTAL_CANCELADO > 0
UNION
SELECT
	TEXT = ''60A'' + CONVERT(VARCHAR, A.DATA_FISCAL, 112) + CONVERT(CHAR(20), A.ID_EQUIPAMENTO) +
	CONVERT(CHAR(4), ''DESC'') + RIGHT(REPLICATE(''0'', 12) + CONVERT(VARCHAR, CONVERT(BIGINT, A.TOTAL_DESCONTO * 100)), 12) + CONVERT(CHAR(79), ''''), 
	CONVERT(VARCHAR, A.DATA_FISCAL, 112) + RTRIM(A.ID_EQUIPAMENTO) + ''2'' AS INDICE 
FROM
	LOJA_CONTROLE_FISCAL A
WHERE
	A.CODIGO_FILIAL = @P16
	AND A.DATA_FISCAL BETWEEN @P17 AND @P18
	AND A.TOTAL_DESCONTO > 0
ORDER BY
	TEXT',N'@P1 varchar(6),@P2 datetime,@P3 datetime,@P4 varchar(6),@P5 datetime,@P6 datetime,@P7 varchar(6),@P8 datetime,@P9 datetime,@P10 varchar(6),@P11 datetime,@P12 datetime,@P13 varchar(6),@P14 datetime,@P15 datetime,@P16 varchar(6),@P17 datetime,@P18 datetime','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000','000888','2009-10-10 00:00:00:000','2009-10-10 00:00:00:000'